<?php
//require_once "weixin_info.php";
require_once "config.php";

/**
 * 微信支付
 */
class Wxsend
{//类定义开始
//点击发送微信客服消息 ,图文消息

    /**
     * [protected description]微信公众号appid
     * @var [type]
     */
    protected $appid = "wx16320d7479a5774c";

    /**
     * [protected description]微信公众号appsecret
     * @var [type]
     */
    protected $appsecret = "e111c965e8d4ba6a2ff1a99724f6d573";

    /**
     * [protected description]收取微信客服消息的openid
     * @var [type]
     */
//    protected $openid = $_POST['openid'];

    public function SendInfo()
    {
        $openid = 'ocj7u1fTiODz-pqIAFGSlX-F04no';//$_POST['openid'];
        $type = $_POST['type'];
        if ($type == 1) {
            $way = '视频通话';
        } elseif ($type == 2) {
            $way = '音频通话';
        } else {
            $way = '';
        }
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret;
        $res = $this->http_curl($url);
        $res = json_decode($res, true);
        var_dump($res);
        die;
        $urls = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $res['access_token'];
//        $nickname = Session::get('wx.NickName');
        $data = '{
            "touser":"' . $openid . '",
            "msgtype":"text",
            "text":
            {
                 "content":"您有新的通话请求，类型是:' . $way . '请于个人中心聊天室按钮进入房间···"
            }
        }';
//        var_dump($data);exit;
        $list = $this->http_curl2($urls, $data);
        var_dump($list);
        die;
    }

    //功能：curl通讯
    private function http_curl($url)
    {
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//初始化curl连接地址，设置要抓取的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置将得到的结果保存在字符串中还是输出在屏幕上，0为直接输出屏幕，非0则不输出
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//对认证证书来源的检查，false表示阻止检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//从证书中检查ssl加密算法是否存在
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);

        // curl_setopt ( $ch, CURLOPT_POST, 1 );
        // curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

        $result = curl_exec($ch);//执行请求获取返回结果
        // $info = curl_getinfo($ch);
        curl_close($ch);//关闭curl会话
        return $result;
    }

    private function http_curl2($url, $data)
    {
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//初始化curl连接地址，设置要抓取的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置将得到的结果保存在字符串中还是输出在屏幕上，0为直接输出屏幕，非0则不输出
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//对认证证书来源的检查，false表示阻止检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//从证书中检查ssl加密算法是否存在
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $result = curl_exec($ch);//执行请求获取返回结果
        // $info = curl_getinfo($ch);
        curl_close($ch);//关闭curl会话
        return $result;
    }


}

class Modelsend
{

    /**
     * [protected description]微信公众号appid
     * @var [type]
     */
    protected $appid = "wx16320d7479a5774c";

    /**
     * [protected description]微信公众号appsecret
     * @var [type]
     */
    protected $appsecret = "e111c965e8d4ba6a2ff1a99724f6d573";


    //获得全局access_token
    public function get_token()
    {//如果已经存在直接返回access_token
        //if($_SESSION['access_token'] && $_SESSION['expire_time']>time()){
        //return $_SESSION['access_token'];
        //}else{
//        //1.请求url地址
//        $appid = APPID;//appid
//        $appsecret =  APPSECRET;//appsecret
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret;//请求地址
        //2初始化curl请求
        $ch = curl_init();
        //3.配置请求参数
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_URL, $url);//请求
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出数据
        //4.开始请求
        $res = curl_exec($ch);//获取请求结果
        if (curl_errno($ch)) {
            var_dump(curl_error($ch));//打印错误信息
        }

        var_dump($res);
        exit;
        //5.关闭curl
        curl_close($ch);
        $arr = json_decode($res, true);//将结果转为数组
        //$_SESSION['access_token']=$arr['access_token'];　　//将access_token存入session中，可以不存，每次都获得新的token
        //$_SESSION['expire_time']=time()+7200;
        return $arr['access_token'];
        //}
    }

    //推送模板信息    参数：发送给谁的openid,客户姓名，客户电话，推荐楼盘（参数自定）
    function sendMessage($openid, $jump_url, $way)
    {
        //获取全局token
        $token = $this->get_token();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;//模板信息请求地址
        //发送的模板信息(微信要求json格式，这里为数组（方便添加变量）格式，然后转为json)
        $post_data = array(
            "touser" => $openid,//推送给谁,openid
            "template_id" => "YH_s1ZKdUPCZ15xJRV_JjQm38S89-Wo632Z5BV0Egms",//微信后台的模板信息id
            "url" => $jump_url,//下面为预约看房模板示例
            "data" => array(
                "first" => array(
                    "value" => "您有新的" . $way . "通话请求，请点击查看！",
                    "color" => "#173177"
                ),
                "keyword1" => array(
                    "value" => date('Y-m-d H:i:s'),//传的变量
                    "color" => "#173177"
                ),
                "keyword2" => array(
                    "value" => '888',
                    "color" => "#173177"
                ),
                "remark" => array(
                    "value" => "请点击该条信息或进入5P8个人中心聊天室！",
                    "color" => "#173177"
                ),
            )
        );
        //将上面的数组数据转为json格式
        $post_data = json_encode($post_data);
        //发送数据，post方式<br>　　　　　　　　　//配置curl请求
        $ch = curl_init();//创建curl请求
        curl_setopt($ch, CURLOPT_URL, $url); //设置发送数据的网址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置有返回值，0，直接显示
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);//禁用证书验证
        curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        //接收执行返回的数据
        $data = curl_exec($ch);
        //关闭句柄
        curl_close($ch);
        $data = json_decode($data, true); //将json数据转成数组
        return $data;
    }

    //获取模板信息-行业信息（参考，示例未使用）
    function getHangye()
    {
        //用户同意授权后，会传过来一个code
        $token = $this->get_token();
        $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" . $token;
        //请求token，get方式
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($data, true); //将json数据转成数组
        //return $data["access_token"];
        return $data;
    }
}

//var_dump('000');exit;
$class = new Wxsend();
$send = new Modelsend();
//$room_num = $_POST['room_num'];
//$phone = $_POST['cellphone'];
//$url = "http://sms.106jiekou.com/utf8/sms.aspx";
//$param = array(
//    'account' => 'sz1jsd',
//    'password' => 'j1sd309930x3y',
//    'mobile' => $phone,
////    'content' => '您的订单编码：888888。如需帮助请联系客服。',
//    'content' => '您的订单编码：' . $room_num . '。如需帮助请联系客服。',
//);
//$res = $class->SendInfo();
$res = $send->sendMessage('ocj7u1fTiODz-pqIAFGSlX-F04no');

echo $res;
